using System;
using Castle.MonoRail.Framework;
using Castle.MonoRail.Framework.Helpers;
using NHibernate.Criterion;
using Rhino.Commons;
using WineCellar.Data;
using WineCellar.Web.Common;
using WineCellar.Web.Filters;

namespace WineCellar.Web.Controllers
{
    [Layout("index"),AuthenticationFilter]
    [Rescue("generalerror"), Resource("wines", "WineCellar.Web.Resources.Wines")]      
    public class WinesController : ApplicationController
    {        
        public void Index()
        {            
        }
        public void Search()
        {
            if (IsAjaxRequest){CancelLayout();}
        	PropertyBag["searchcriteria"] = Utility.GetQueryCriteria(typeof (Wine));
        }
        public void New()
        {
            PropertyBag["regions"] = Repository<Region>.FindAll();
            PropertyBag["raisins"] = Repository<Raisin>.FindAll();
            PropertyBag["colortypes"] = Enum.GetNames(typeof (Enums.ColorType));
            PropertyBag["wine"] = new Wine();            
        }
        public void Create([DataBind("wine",Validate = true)]Wine wine)
        {
            if (!HasValidationError(wine))
            {
                
				wine.AddedBy = GetLoginUser();
                wine.DateAdded = DateTime.Now;
            	Repository<Wine>.Save(wine);
            	wine.Region = Repository<Region>.Load(wine.Region.Id);                
                PropertyBag["updatedObject"] = wine;
            }
            else
            {
                Flash["updatedObject"] = wine;
            	Flash["summary"] = GetErrorSummary(wine);               
            }
            RenderView("shared", "create");
        }
        public void Edit(int id)
        {
            if (IsAjaxRequest) { CancelLayout(); }
        	Wine wine = Repository<Wine>.Load(id);
            PropertyBag["wine"] = wine;
			PropertyBag["regions"] = Repository<Region>.FindAll();
			PropertyBag["raisins"] = Repository<Raisin>.FindAll();
            PropertyBag["colortypes"] = Enum.GetNames(typeof(Enums.ColorType));				
        }
        public void Delete(int id)
        {
            Wine wine = Repository<Wine>.Load(id);
            PropertyBag["deletedObject"] = wine;
        	With.Transaction(delegate { Repository<Wine>.Delete(wine); });       				
            RenderView("shared", "delete");
        }                                		        
        public void List(string searchcriterium, string criteriaValue)
        {
            if (IsAjaxRequest) { CancelLayout(); }
            PropertyBag["criteriaValue"] = criteriaValue;
        	PropertyBag["searchcriterium"] = searchcriterium;
        	PropertyBag["searchcriteria"] = Utility.GetQueryCriteria(typeof (Wine));
			if (!string.IsNullOrEmpty(criteriaValue))
			{
				PropertyBag["wines"] = PaginationHelper.CreatePagination(this.Context, Repository<Wine>.FindAll(new ICriterion[] { Expression.Like(searchcriterium, criteriaValue) }), 5);
			}
			else
			{
				PropertyBag["wines"] = PaginationHelper.CreatePagination(this.Context, Repository<Wine>.FindAll(),5);				
			}
            
        }
        public void View(int id)
        {
            if (IsAjaxRequest) { CancelLayout(); }
        	PropertyBag["wine"] = Repository<Wine>.Load(id);
        }
        public void Update([DataBind("wine",Validate=true)] Wine wine)
        {           
            if (!HasValidationError(wine))
            {
            	With.Transaction(delegate
            	                 	{
										Repository<Wine>.SaveOrUpdate(wine);	
            	                 	});				
            	wine.Region = Repository<Region>.Load(wine.Region.Id);
                PropertyBag["updatedObject"] = wine;
            }
            else
            {
                Flash["updatedObject"] = wine;
            	Flash["summary"] = GetErrorSummary(wine);                                
            }
            RenderView("shared","update");
        }                          
        public void List()
        {
            List("name","%");
        }        
    }
}
